<?php

namespace App\Http\Controllers\Api\CatchOrder;

use App\Http\Controllers\Api\BaseController;
use App\Jobs\OrderSubmit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class CatchPddOrderController extends BaseController
{
    public $log;

    public function __construct()
    {
        $this->log = new Logger('PddPushCatchData');
        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/PddPushCatchData.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );
    }

    //
    public function pushOrder(Request $Request)
    {
        $getData = file_get_contents('php://input');
        $this->log->info('input' . ($getData));
        // var_dump(1111111111);  // 店铺好 和订单号
        $getData = '{"mall_id": 1511532,"tid": "20124-2012455211122"}';
        $getDatas = json_decode($getData, true);
        $shop_id = 0;
        $pid = '';
        if (!empty($getDatas)) {
            foreach ($getDatas as $key => $v) {
                if (!empty($v['data']) && is_string($v['data'])) {

                    $arrays = json_decode($v['data'], true);
                    // var_dump($arrays);
                    // 判断下状态
                    $flag = true;
                    $shop_id = !empty($arrays['mall_id']) ? $arrays['mall_id'] : '';
                    $pid = !empty($arrays['tid']) ? $arrays['tid'] : '';
                    if (!empty($shop_id) && !empty($pid)) {
                        $data = [
                            'shop_id' => $shop_id,
                            'pid' => $pid,
                            'type' => 'pdd'
                        ];
                        // 将数据存入到表中   防止遗漏 验证
                        $res = DB::table('pdd_catch_verific')->where(['pid' => $pid])->first();
                        if (!$res) {
                            $datas = [
                                'pid' => $pid,
                                'shop_id' => $shop_id,
                                'created_at' => date('Y-m-d H:i:s'),
                                'is_ver' => 1
                            ];
                            DB::table('pdd_catch_verific')->insert($datas);
                            // var_dump($data);
                            dispatch(new OrderSubmit($data, 'catchOrder', 60))->onQueue('TaskOrderSubmitDispose');
                        }
                    }
                }
            }
        }
        $str = '{"result":true}';
        $this->log->info('输入的数据' . $getData . '返回的参数' . $str);
        return $str;
    }

    public function upThree($ordernum = '', $type = 'catchOrder')
    {
        dispatch(new OrderSubmit($ordernum, $type))->onQueue('TaskOrderSubmitDispose');
        return 1;
    }
}
